<template>
  <el-row class="login-container">
    <el-col :sm="12" :lg="16" class="left">
      <div>
        <div class="font-bold text-5xl text-light-50 mb-4">欢迎使用!</div>
        <div class="text-gray-200 text-sm">列车订票系统</div>
      </div>
    </el-col>

    <el-col :sm="12" :lg="8" class="right">
      <img style="width: 200px; height: 200px; margin-top: 20px; margin-bottom: 10px;" src="../../../assets/img/logo.png"
        alt="" />
      <h2 class="font-bold text-3xl text-gray-800">用户登录</h2>
      <div class="flex items-center justify-center my-5 text-gray-300 space-x-2">
        <span class="h-[1px] w-16 bg-gray-200"></span>
        <span>账户密码登录</span>
        <span class="h-[1px] w-16 bg-gray-200"></span>
      </div>

      <el-form :model="form" class="w-[250px]" :rules="rules">
        <el-form-item prop="userAccount">
          <el-input v-model="form.userAccount" placeholder="手机号" prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>

        <el-form-item prop="userPassword">
          <el-input type="userPassword" v-model="form.userPassword" placeholder="密码" prefix-icon="el-icon-lock"
            show-password>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="agree">我已同意"列车订票系统"用户隐私协议</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button class="login-button" @click="setLogin" :loading="loading">登录</el-button>
          <el-link href="/#/regist" style="font-weight: bolder;font-size: 15px;color: #374151;" :underline="false">
            去注册
            <i style="font-weight: bolder;font-size: 15px" class="el-icon-right"></i>
          </el-link>
          <!-- <el-button>Cancel</el-button> -->
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>


<script scope>
import { toast } from '../../../composables/util'

export default {
  name: 'Login',
  data() {
    return {
      agree: false,
      loading: false,
      form: {
        userAccount: "15872079615",
        userPassword: "12345678"
      },
      rules: {
        userAccount: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          { min: 11, max: 11, message: '账号应为11位', trigger: 'change' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 8, max: 16, message: '密码应为8-15位', trigger: 'change' }
        ]
      }
    }
  },

  methods: {
    login() {
      this.request.post("/user/loginUser", this.form).then(
        response => {
          if (response.data.code === 1) {
            // console.log(response)
            const user_info = {
              id: response.data.data.id,
              userAccount: response.data.data.userAccount,
              userName: response.data.data.userName,
              userEmail: response.data.data.userEmail
            }
            sessionStorage.setItem('login_user', JSON.stringify(user_info))
            toast("登录成功", this)
            this.$router.push({ name: "Home" })
          } else {
            // console.log(response)
            toast("账户或密码错误", this, "error")
          }
        },
        error => {
          console.log(error.message)
        }
      )
        .finally(() => {
          this.loading = false
        })
    },

    setLogin() {
      if (this.agree === false) {
        this.$message({
          message: '请勾选 我已同意"列车订票系统"用户隐私协议!',
          type: 'warning'
        });
        return
      }
      this.loading = true
      setTimeout(() =>
        this.login()
        , 1000);
    }
  }
}
</script>

<style scoped>
.login-container {
  @apply min-h-screen;
  background-image: -moz-linear-gradient(135deg, rgb(14, 0, 209), rgb(5, 158, 209));

  background-image: -webkit-linear-gradient(135deg, rgb(14, 0, 209), rgb(5, 158, 209));

  background-image: linear-gradient(135deg, rgb(14, 0, 209), rgb(5, 158, 209));
}

.login-container .left {
  @apply flex items-center justify-center min-h-screen;
}

.login-container .right {
  @apply flex items-center min-h-screen;
}

.login-container .right {
  @apply bg-light-100 flex-col;
}

.regist-item-text {
  font-size: 20px;
}

.login-button {
  width: 100px;
  margin-right: 10px;
  margin-left: 75px;
  background-image: -moz-linear-gradient(135deg, rgb(14, 0, 209), rgb(5, 158, 209));

  background-image: -webkit-linear-gradient(135deg, rgb(14, 0, 209), rgb(5, 158, 209));

  background-image: linear-gradient(135deg, rgb(14, 0, 209), rgb(5, 158, 209));
  color: #fcfcfc;
}
</style>
